package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.BaseBean;
import com.meihua.baoleyi.utils.CountDownTimerUtils;
import com.meihua.baoleyi.utils.JsonUtils;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;
/**
 * Created by liming on 2017/10/17
 */
public class ResetPayPwdVerifyPhoneActivity extends BaseActivity {
    Toast toast;
    @Bind(R.id.codede)
    EditText codede;
    @Bind(R.id.show_zhanghao)
    TextView tvShowZhaohao;
    @Bind(R.id.btn_sent_verify_code)
    Button btnSendVerifyCode;
    @Bind(R.id.text_back)
    ImageView textBack;
    @Bind(R.id.text_title)
    TextView textTitle;
    private String phone;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pay_pwd_verify_phone);
        ButterKnife.bind(this);
        initView();

    }
    private void initView() {
        textTitle.setText("验证手机号");
        phone = myApp.getPhone().toString();
        phone = phone.replace(phone.substring(3, 7), "****");
        tvShowZhaohao.setText(phone);
    }

    @OnClick({R.id.text_back, R.id.btn_sent_verify_code, R.id.reset_paypwd_submit})
    public void OnViewClicked(View v) {
        switch (v.getId()) {
            case R.id.text_back: //返回
                finish();
                break;
            case R.id.btn_sent_verify_code:
               sendVerifyCode();
                break;
            case R.id.reset_paypwd_submit:
                submit();
                break;
        }
    }

    /**
     * 发送验证码
     */
    private void sendVerifyCode() {
        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token=myApp.getToken().toString();
        String sign_none="phone="+myApp.getPhone().toString()+"&timestamp="+str+"&token="+token;
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();

        OkGo.get(UrlContants.HOST+"api/account/sendPay?phone="+myApp.getPhone().toString()+"&timestamp="+str+"&token="+token+"&sign="+sign_yes)
                .tag(this)
                .params("phone",myApp.getPhone().toString())
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
//                            Log.e("sdfujasfads", s + "");
                            JSONObject josn = new JSONObject(s);
                            if (josn.getString("code").equals("200")) {
                                showShortCenter(ResetPayPwdVerifyPhoneActivity.this, josn.getString("msg"));
                                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(ResetPayPwdVerifyPhoneActivity.this, btnSendVerifyCode, 60000, 1000);
                                countDownTimerUtils.start();
                            } else {
                                showShortCenter(ResetPayPwdVerifyPhoneActivity.this, josn.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


        CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(ResetPayPwdVerifyPhoneActivity.this, btnSendVerifyCode, 60000, 1000);
        countDownTimerUtils.start();
    }

    public void showShortCenter(Context context, String content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(content);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }

    private void submit() {
        if (TextUtils.isEmpty(codede.getText()) || TextUtils.isEmpty(codede.getText().toString().trim())) {
            showShortCenter(this, "请输入验证码");
            return;
        }

        long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳
        String str=String.valueOf(time);
        String token=myApp.getToken().toString();
        String sign_none="code="+codede.getText().toString().trim()+"&timestamp="+str+"&token="+token+"&user_phone="+myApp.getPhone().toString();
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();

        OkGo.get(UrlContants.HOST+"api/account/verifyCode?code="+codede.getText().toString().trim()+"&timestamp="+str+"&token="+token+"&user_phone="+myApp.getPhone().toString()+"&sign="+sign_yes)
                .tag(this)
                .params("code",codede.getText().toString().trim())
                .params("timestamp",str)//时间戳
                .params("token",token)//token
                .params("user_phone",myApp.getPhone().toString()) //用户手机号码
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Object obj = JsonUtils.parse(ResetPayPwdVerifyPhoneActivity.this,s, BaseBean.class);
                        if(obj != null){
                            if(obj instanceof BaseBean){
                                BaseBean bean = (BaseBean) obj;
                                if(bean.getCode().equals("200")) {
                                    startActivity(new Intent(ResetPayPwdVerifyPhoneActivity.this, PayPwdActiity.class));
                                    finish();
                                }else {
                                    showShortCenter(ResetPayPwdVerifyPhoneActivity.this, bean.getMsg());
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        ToastUtil.show(ResetPayPwdVerifyPhoneActivity.this,"服务器异常！");
                    }
                });
    }

}
